

#include <stddef.h>
#include <pthread.h>

#ifdef __cplusplus
extern "C" {
#endif

#define MAX_MSG_SIZE (4 * 1024)
#define BUFFER_POOL_SIZE (2 * 1024)

typedef struct {
    char data[MAX_MSG_SIZE];
    ssize_t length;
} data_buffer_t;

typedef struct {
    data_buffer_t *buffers[BUFFER_POOL_SIZE];
    int count;
    int front;
    int rear;
    pthread_mutex_t mutex;
} buffer_pool_t;

typedef struct {
    data_buffer_t *queue[BUFFER_POOL_SIZE];
    int count;
    int front;
    int rear;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} mssg_queue_t;

void init_buffer_pool(void);
data_buffer_t *get_buffer(void);
void return_buffer(data_buffer_t *buf);
void deinit_buffer_pool(void);

void init_message_queue(void);
void enqueue_message(data_buffer_t *buf);
void notify_queue_message(void);
void deinit_message_queue(void);
data_buffer_t *dequeue_message_timed(int timeout_ms);

#ifdef __cplusplus
}
#endif
